import request from "./request";
import { API_URL } from "../constants";
import type {
  IDeviceFixModeRunningStatus,
  IDeviceProgramModeRunningStatus,
  IDeviceStatus,
  IResponse,
  IResponseList,
} from "../types";
import type { EChartsCoreOption } from "echarts";

export function getDeviceStatusList(token: string, signal?: AbortSignal) {
  return request<IResponse<{ data: IDeviceStatus[] }>>(
    `${API_URL}/business/deviceStatus/list`,
    {
      headers: { Authorization: token },
      signal,
    },
  );
}

export function getDeviceStatus(id: number, token: string) {
  return request<IResponse<{ data: IDeviceStatus[] }>>(
    `${API_URL}/business/deviceStatus/${id}`,
    {
      headers: { Authorization: token },
    },
  );
}

export function updateDeviceMode(
  data: { deviceId: number; mode: "fix" | "program" },
  token: string,
) {
  return request<IResponse>(`${API_URL}/business/deviceStatus/change/mode`, {
    method: "post",
    data,
    headers: { Authorization: token },
  });
}

export function getDeviceRunningStatus(
  id: number,
  token: string,
  signal?: AbortSignal,
) {
  return request<
    IResponse<{
      data: IDeviceFixModeRunningStatus | IDeviceProgramModeRunningStatus;
    }>
  >(`${API_URL}/business/deviceStatus/run/info/${id}`, {
    headers: { Authorization: token },
    signal,
  });
}

export function resetDeviceStatus(deviceId: number, token: string) {
  return request<IResponse>(`${API_URL}/business/deviceStatus/reset`, {
    params: { deviceId } as any,
    headers: { Authorization: token },
  });
}

export function runDevice(deviceId: number, token: string) {
  return request<IResponse>(`${API_URL}/business/deviceStatus/run`, {
    params: { deviceId } as any,
    headers: { Authorization: token },
  });
}

export function keepDevice(deviceId: number, token: string) {
  return request<IResponse>(`${API_URL}/business/deviceStatus/keep`, {
    params: { deviceId } as any,
    headers: { Authorization: token },
  });
}

export function stopDevice(deviceId: number, token: string) {
  return request<IResponse>(`${API_URL}/business/deviceStatus/stop`, {
    params: { deviceId } as any,
    headers: { Authorization: token },
  });
}

export function nextStep(deviceId: number, token: string) {
  return request<IResponse>(`${API_URL}/business/deviceStatus/next`, {
    params: { deviceId } as any,
    headers: { Authorization: token },
  });
}

export function continueDevice(deviceId: number, token: string) {
  return request<IResponse>(`${API_URL}/business/deviceStatus/continue`, {
    params: { deviceId } as any,
    headers: { Authorization: token },
  });
}

type JumpStepData = {
  deviceId: number;
  loop: number;
  segmentLoop: number;
  segmentNo: number;
  stepNo: number;
};
export function jumpStep(data: JumpStepData, token: string) {
  return request<IResponse>(`${API_URL}/business/deviceStatus/jump`, {
    method: "post",
    data,
    headers: { Authorization: token },
  });
}

type GetDeviceRunningStatusParams = {
  deviceId?: number;
  pageNum: number;
  pageSize: number;
  "readTime.end"?: string;
  "readTime.start"?: string;
};
export function getDeviceRunningStatusList(
  params: GetDeviceRunningStatusParams,
  token: string,
  signal?: AbortSignal,
) {
  return request<
    IResponseList<{ rows: (IDeviceStatus & { sampleTime: string })[] }>
  >(`${API_URL}/business/deviceStatus/run/read/list`, {
    params: params as any,
    headers: { Authorization: token },
    signal,
  });
}

export function getDeviceRunningStatusAsExcel(
  params: GetDeviceRunningStatusParams,
  token: string,
  signal?: AbortSignal,
) {
  return request<Blob>(`${API_URL}/business/deviceStatus/run/read/export`, {
    method: "post",
    params: params as any,
    headers: { Authorization: token },
    signal,
  });
}

export function getDeviceRunningChartRealtime(
  params: { deviceId: number; isMonitor?: "0" | "1" },
  token: string,
  signal?: AbortSignal,
) {
  return request<IResponse<{ data: EChartsCoreOption }>>(
    `${API_URL}/business/deviceStatus/chart/realtime`,
    {
      params: params as any,
      headers: { Authorization: token },
      signal,
    },
  );
}

export function getDeviceChartHistory(
  params: {
    deviceId: number;
    interval: number;
    endTime?: string;
    orderNo?: string;
    startTime?: string;
    isMonitor?: "0" | "1";
  },
  token: string,
  signal?: AbortSignal,
) {
  return request<IResponse<{ data: EChartsCoreOption }>>(
    `${API_URL}/business/deviceStatus/chart/history`,
    {
      params: params as any,
      headers: { Authorization: token },
      signal,
    },
  );
}

export function toggleDeviceRunningStatus(
  params: { deviceId: number; status: "0" | "1" },
  token: string,
  signal?: AbortSignal,
) {
  return request<IResponse<{ data: boolean }>>(
    `${API_URL}/business/device/change/status`,
    {
      method: "post",
      params: params as any,
      headers: { Authorization: token },
      signal,
    },
  );
}

export function updateDeviceControl(
  params: {
    deviceId: number;
    controlType: "device" | "system";
  },
  token: string,
  signal?: AbortSignal,
) {
  return request<IResponse<{ data: boolean }>>(
    `${API_URL}/business/deviceStatus/confirmTakeControl`,
    {
      params: params as any,
      headers: { Authorization: token },
      signal,
    },
  );
}
